package com.student.education.service.impl;

import com.github.pagehelper.PageHelper;
import com.student.education.common.CommonPage;
import com.student.education.common.CommonResult;
import com.student.education.entity.Clazz;
import com.student.education.entity.User;
import com.student.education.dao.UserDao;
import com.student.education.entity.UserDownload;
import com.student.education.service.UserService;
import com.student.education.to.UserTo;
import com.student.education.vo.MyStudentVo;
import com.student.education.vo.UserQueryVo;
import com.student.education.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 用户表(User)表服务实现类
 *
 * @author makejava
 * @since 2021-09-14 16:25:52
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public UserTo queryById(Integer id) {
        UserTo userTo = userDao.queryById(id);
        if (Objects.equals(userTo.getClassId(),null)){
            return userTo;
        }
        userTo.setClassIdList(Arrays.asList(userTo.getClassId().split(","))
                .stream().map(s -> Integer.parseInt(s.trim()))
                .collect(Collectors.toList()));
        return userTo;
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(User user) {
        user.setClassId(StringUtils.join(user.getClassIdList().toArray(), ","));
        return this.userDao.insert(user);
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public UserTo update(User user) {
        this.userDao.update(user);
        return this.queryById(user.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.userDao.deleteById(id) > 0;
    }

    /**
     * 登录
     * @param vo
     * @return
     */
    @Override
    public UserTo queryByLogin(UserVo vo) {
        int i = userDao.queryByLogin(vo);
        if (!Objects.equals(i,1)){
            return null;
        }
        UserTo userTo = userDao.queryById(vo.getUserId());
        if (Objects.equals(userTo.getClassId(),null)){
            return userTo;
        }
        userTo.setClassIdList(Arrays.asList(userTo.getClassId().split(","))
                .stream().map(s -> Integer.parseInt(s.trim()))
                .collect(Collectors.toList()));
        return userTo;
    }

    @Override
    public int updateMyInfo(User vo) {
        vo.setClassId(StringUtils.join(vo.getClassIdList().toArray(), ","));
        return userDao.updateMyInfo(vo);
    }

    @Override
    public CommonPage<UserTo> queryAll(UserQueryVo vo) {
        if (vo.getPageUtil() != null) {
            PageHelper.startPage(vo.getPageUtil().getPageNumber(), vo.getPageUtil().getPageSize());
        }
        List<UserTo> userTos = userDao.queryAll(vo);
        for (int i =0;i<userTos.size();i++){
            if (Objects.equals(userTos.get(i).getClassId(),null)){
                continue;
            }
            userTos.get(i).setClassIdList(Arrays.asList(userTos.get(i).getClassId().split(","))
                    .stream().map(s -> Integer.parseInt(s.trim()))
                    .collect(Collectors.toList()));
        }
        return CommonPage.restPage(userTos);
    }

    @Override
    public int delete(List<Integer> ids) {
        return userDao.delete(ids);
    }

    @Override
    public CommonPage<UserTo> queryMyStudents(MyStudentVo vo) {
        String classId = userDao.queryClassId(vo.getId());
        if (vo.getPageUtil() != null) {
            PageHelper.startPage(vo.getPageUtil().getPageNumber(), vo.getPageUtil().getPageSize());
        }
        List<UserTo> list = userDao.queryMyStudents(vo,classId);
        return CommonPage.restPage(list);
    }

    @Override
    public List<Clazz> queryMyClass(Integer id) {
        String classId = userDao.queryClassId(id);
        return userDao.queryMyClass(classId);
    }

    @Override
    public int insertDD(List<UserDownload> list) {
        int z=0;
        List<User> classIds = userDao.queryClassByName(list);
        for (int i=0;i<list.size();i++){
            if (Objects.equals(classIds.get(i).getClassId(),null)){
                z++;
                list.remove(i);
                continue;
            }
            list.get(i).setClassId(classIds.get(i).getClassId());
        }
        userDao.insertUser(list);
        return z;
    }
}